<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\GoodsSeckill;
use App\Models\GoodsSeckillConfig;
use App\Models\StoreGood;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class GoodsSeckillController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new GoodsSeckill(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('config_id');
            $grid->column('action_time','秒杀开始时间');
            $grid->column('finish_time','秒杀结束时间');
            $grid->column('goods_id');
            $grid->column('activity_title','活动标题');
            $grid->column('activity_text');
            $grid->column('seckill_price');
            $grid->column('seckill_goods_store','库存');
            $grid->column('seckill_goods_residue','库存剩余');
            $grid->column('seckill_status');
            $grid->seckill_status
                ->using([1 => '关闭', 0 => '已结束'])
                ->dot(
                    [
                        1 => '',
                        0 => 'danger',
                    ],
                    'primary' // 默认颜色
                );
            $grid->column('status')->switch('', true);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new GoodsSeckill(), function (Show $show) {
            $show->field('id');
            $show->field('config_id');
            $show->field('action_time');
            $show->field('finish_time');
            $show->field('goods_id');
            $show->field('activity_title');
            $show->field('activity_text');
            $show->field('seckill_price');
            $show->field('seckill_goods_store');
            $show->field('seckill_goods_residue');
            $show->field('seckill_status');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new GoodsSeckill(), function (Form $form) {
            $form->display('id');
//            $form->text('config_name');
            $form->select('config_id','配置')->options('config');
            $form->datetime('action_time')->format('YYYY-MM-DD HH:mm:ss');
            $form->datetime('finish_time')->format('YYYY-MM-DD HH:mm:ss');
            $form->select('goods_id')->options('goods');
            $form->text('activity_title');
            $form->text('activity_text');
            $form->text('seckill_price');
            $form->text('seckill_goods_store');
            $form->text('seckill_goods_residue');
            $form->text('seckill_status');
            $form->text('status');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }


    public function config(){
        $data = GoodsSeckillConfig::select('id','config_name')->get();
        foreach ($data as $k=>$v){
            $datas[$k]['id']=$v['id'];
            $datas[$k]['text'] =$v['config_name'];
        }
        return $datas;
    }

    public function goods(){
        $data = StoreGood::select('id','store_name')->get();
        foreach ($data as $k=>$v){
            $datas[$k]['id']=$v['id'];
            $datas[$k]['text'] =$v['store_name'];
        }
        return $datas;
    }
}
